<?php
namespace app\models;

use Yii;
use yii\db\ActiveRecord;

class comment extends ActiveRecord{
	public static function tableName(){
		return "{{%comment}}";
	}
	
	public function rules(){
		return[
		    ['comment','required','message' => '提交的内容不能为空~'],
			['comment','string','max' => 600],
		   ];
	   }
	  
//comment表有comment_id, content_id,openid,comment,state,nickname,createtime.
	public function add($data){
		if($data){
		  
		  //创建时间
		  date_default_timezone_set('PRC');
		  $createtime = date('Y-m-d H:i:s');
		  
		  //输入数据库
		  $this->comment = $data['content'];
		  $this->content_id = $data['id'];
		 // $this->nickname = $nickname;//默认北鼻
		  $this->createtime = $createtime;
		  $this->openid = $data['username'];
		  $result = $this->save();
		  if($result){
			  $error = 0;
			  return $error ;
		  }else{
			  $error = $this->getErrors();
			  return $error;
		  }
		}else{
			$error = 2;
			return $error;
		}
	}
           //改评论的state=1表示已读 将不会在收信栏出现
         public function del($data){
                  if($data){
                    $comment_id = $data['id'];
                    $State =  comment::findOne($comment_id);
                    $State->state = '1';
                    $result = $State->save();
                    if($result){
                         $error = 0;
                         return $error;
                     } else{
                          $error = $this->getErrors();
                          return $error;
                     }
                  }else{
                          $error = 2;
                          return $error;
                   }
             }
                     	  
}
